<?php
App::import('vendor','paginator');
class CronController extends AppController
{
	var $name = "Cron";
	var $uses = array('Cron','Page');
	var $components = array('Session');
	var $currency = "&amp;euro;";
	var $html_currency = "&euro;";
	var $updatePassword = "a56f8ebcc75ab7655ced6d9d";

	function insertRegions(){
		$game_id   = $this->Session->read('game_id');
		$game_info = $this->Page->getGameInfo($game_id);

		$slice_lat  = ($game_info['max_lat'] - $game_info['min_lat'])/9.5;
		$slice_long = ($game_info['max_long'] - $game_info['min_long'])/9.5;	
		for($i=$game_info['min_lat']; $i<$game_info['max_lat']; $i += $slice_lat){
			for($j=$game_info['min_long']; $j<$game_info['max_long']; $j += $slice_long){
				$this->Ajax->insertRegions($i,($i+$slice_lat),$j,($j+$slice_long),$game_id);				
			}
		}
		exit();
	}

	function createMarkers($with_exit = 0){
		$games = $this->Cron->getGameIds();
		foreach($games as $game) {
			$markers = $this->Cron->getMarkers($game['game_id']);
			$XMLdoc  = fopen('MarkerData'.$game['game_id'].'.xml', 'w');
			fwrite($XMLdoc, '<markers>');	
			foreach($markers as $mark){
				$mark_value = $this->Cron->getValue4Building((int)$mark["building_condition"],
					(int)$mark["region_worth"],$mark["building_type"]);				
				$region_id = $mark["region_id"] % 100;	
				fwrite($XMLdoc, "<marker lat='{$mark["pos_lat"]}' lng='{$mark["pos_long"]}' ".
					"html=\"&lt;table&gt;&lt;tr&gt;&lt;td&gt;".
						"&lt;div style='width:200px; font-family: Arial; font-size:8px;'&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Region:&lt;/span&gt; {$region_id}&lt;/p&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Property:&lt;/span&gt; {$mark["property_name"]}&lt;/p&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Owner:&lt;/span&gt; ". 
							"&lt;a href='".U_PROFILES.'#profile'.$mark["user_id"]."'&gt;{$mark["first_name"]} {$mark["last_name"]}&lt;/a&gt;".
						"&lt;/p&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Building:&lt;/span&gt; {$mark["building_type"]}&lt;/p&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Revenue:&lt;/span&gt; {$this->currency} {$mark["revenue"]}/day&lt;/p&gt;".
						"&lt;p&gt;&lt;span class='strong'&gt;Building value:&lt;/span&gt; {$mark["price_buy"]} {$this->currency}&lt;/p&gt;".
						(($mark["forsale"]==1)?
							"&lt;p&gt;&lt;span class='strong'&gt;Buy Price:&lt;/span&gt; ".
							$mark_value.' '.$this->currency:"") 	
						."&lt;p&gt;&lt;span class='strong'&gt;Condition:&lt;/span&gt; {$mark["building_condition"]}&lt;/p&gt;".
						"&lt;/div&gt;".
					"&lt;/td&gt;&lt;td&gt;\" 
					label='{$mark["user_id"]}' 
					building='{$mark["building_type"]}'  
					action1 = \"&lt;div style='text-align: center;'&gt;".
							"&lt;img src='{$mark["img_url"]}' style='border:2px solid #cecece;' &gt;
						 	&lt;div&gt;".
							 (($mark["forsale"]==1)? 	
					 		"&lt;a onclick='buyProperty({$mark["marker_id"]},{$mark_value});return false;'&gt;\n         
					 	 		&lt;input type='button' value='\$Buy\$'&gt;\n 
					 	 	&lt;/a&gt;":"").
					 	 	"&lt;a href='".U_HOME."Pages/destroyProperty/{$mark["marker_id"]}/{$mark['user_id']}/' &gt;\n         
					 			&lt;input type='button' value='\$Destroy\$'&gt;\n     
							&lt;/a&gt;\n".
					 	 	"&lt;/div&gt;\n".        
					    "&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;".
					"&lt;/table&gt;\" 
					action2 = \"&lt;div style='text-align: center;'&gt;".
						((int)$mark["building_condition"]<100 ? 
							"&lt;div&gt;&lt;a href='".U_HOME."Pages/upgradeProperty/{$mark["marker_id"]}/{$mark['user_id']}/' &gt;\n         
					 			&lt;input type='button' value='\$Buy an upgrade\$'&gt;\n     
							&lt;/a&gt;&lt;/div&gt;\n":"").
						 
						"&lt;img src='{$mark["img_url"]}' style='border:2px solid #cecece;' &gt;".
						"&lt;div&gt;Set for sale:
							&lt;input type='checkbox' id='forsale' name='forsale' 
							".($mark["forsale"]==1?" checked='checked' ":"")."
							onclick='setForSale({$mark['property_id']})' value='{$mark["forsale"]}' /&gt;
						&lt;/div&gt;\n".
					    "&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;".
					"&lt;/table&gt;\" />\n");			
			}
			fwrite($XMLdoc, '</markers>');
			fclose($XMLdoc); 
		}	
		if($with_exit == 1){exit();}
	}
	
	function makeUpdates(){
		$this->Cron->cleanNewsTable();
		$this->Cron->cleanChat();
		$this->createMarkers();
	}
	
	function getUpgradePrice($building_cond,$old_cond){
		$upgrade_val = $this->Cron->getValue4Update($building_cond,$old_cond);
		echo $upgrade_val.' '.$this->html_currency;	
		exit();	
	}

	function initSwarm($game_id) {
		//calls the regionsWorthUpdate, moneyUpdate, buildingValueUpdate, updateSwarm
		$this->Cron->initializeSwarm($game_id);
	}

	function updateGames($pw) {
		if (isset($pw) && !strcmp($pw, $this->updatePassword)) {
			$this->Cron->updateSwarms();
			$this->Cron->regionsWorthUpdate();
			$this->Cron->buildingValueUpdate();
			$this->Cron->getMoneyUpdates();
			exit(); // mainly for windows...
		} else {
			echo "ERROR! What are you doing here anyway???";
		}
	}
}